mini.parse();
var modell=localStorage.getItem("id");
console.log(modell)
var grid = mini.get("tableClass");//主表



$.ajax({
    url: ajaxUrl + "/formltems/findByModelId.do",
    type: "POST",
    data:{
        modelId:modell
    },
    success: function (data) {
        console.log(data);
        data5=mini.clone(data);
        data7=mini.clone(data)
        if(data.formItem[0]!=null){
            var cTableName=data.formItem[0].cTableName;
        }
        localStorage.setItem("datayl",JSON.stringify(data5));
        localStorage.setItem("cTableName",JSON.stringify(cTableName));
        console.log(cTableName);
        // mainTreeData = dbTree(grid,data.formItem);//获取主表树
        grid.setData(data.formItem);
    },
    error : function(data){
    }
});

//表格树数据加载
var dbData = [];//存放转换后的数据库表数据
function dbTree(grid1,data) {
    dbData = [];
    console.log(data,'data');
    for(var i=0;i<data.length;i++) {
        dbData[i] = {
            tableClassName: data[i].tableClassName,
            tableChClassName: data[i].tableChClassName,
            cTableName: data[i].tableChClassName,
            field: '',
            fieldCh: '',
            tableClassId: data[i].tableClassId + '',//id
            PID: -1,
            checked: false,
//            expanded: true,
            children:[]
        };
        for(var j=0;j<data[i].children.length;j++) {
            var c = data[i].children[j];
            dbData[i].children.push({
                // cTableField: c.field,
                cTableName: data[i].tableChClassName,
                tableClassName: data[i].tableClassName,
                // formListId: data[i].formListId,
                type: c.type,
                field: c.field,
                fieldCh: c.fieldCh,
                tableClassId: c.tableId + '',//id
                PID: data[i].tableClassId + '',
//                expanded: true,
                checked: false
            });
        }
    }
    console.log(dbData,'dbData');
    grid1.setData(dbData);
    return dbData;
}
//弹出
function alertBox() {
    mini.alert('请选择用户');
}
//选中的数据存入数组
var select_data = [];
function getSelectData(record) {
    var flag = true;
    for(var i=0;i<record.length;i++) {
        for(var j=0;j<select_data.length;j++) {
            if (record[i].cTableField == select_data[j].cTableField) {
                flag = false;
            }
        }
    }
    if(flag && record.PID != -1) {
        select_data.push(record);
    }
    console.log(select_data,'select_data');
}
grid.on("rowdblclick", function (e) {
    console.log(e.record);
    insertAtCursor(e.record.cTableField_ch,true);
    getSelectData(e.record);

});
// SetData1();

//插入[]光标
function insertAtCursor(myValue,calc) {
    var myField = document.getElementById('textarea');
    if(calc) {//若为变量，加[]
        myValue = '['+myValue+']';
    }

    //IE 浏览器
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
        sel.select();
    }

    //FireFox、Chrome等
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;

        // 保存滚动条
        var restoreTop = myField.scrollTop;
        myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);

        if (restoreTop > 0) {
            myField.scrollTop = restoreTop;
        }

        myField.focus();
        myField.selectionStart = startPos + myValue.length;
        myField.selectionEnd = startPos + myValue.length;
    } else {
        myField.value += myValue;
        myField.focus();
    }
}

//回显汉字
// function SetData1() {
//
//     var iframedata=localStorage.getItem("iframedata");
//     console.log(iframedata);
//     if(iframedata==null){
//         ;
//     }
//     iframedata=JSON.parse(iframedata);
//     var data = mini.clone(iframedata);
//     console.log(data);
//     if(data!= undefined) {
//
//         if(data!= "" && data!= "\"\"") {
//             insertAtCursor(data.textarea,false);
//             select_data=data.select_data;
//         }
//     }
// }
// 确定
function onOk(){
    select_data=select_data;
    console.log(select_data);
    var textarea=$("textarea").val();//获取textarea上的值
    var newStr = textarea;
    var newStrEn = textarea;
    console.log(textarea);
    console.log(textarea.match(/[^\[]+(?=\])/g));
    console.log(typeof textarea.match(/[^\[]+(?=\])/g));

    if(textarea == "") {
        return "";
    }else {
        $.each(textarea.match(/[^\[]+(?=\])/g), function(i, word) {
            // if(select_data.length==0){     //再次编辑的情况下
            //
                console.log(data7)
                data7.formItem.forEach(function (items) {
                    if(word==items.cTableField_ch){
                        var re_word = word+'.'+items.cTableField+'.'+items.tableClassName;
                        newStr = newStr.replace(word, re_word);
                        var re_word_en = items.cTableField;
                        newStrEn = newStrEn.replace(word, re_word_en);
                    }
                })
            // }else {                         //第一次设置的情况下
            //     for(var i=0;i<select_data.length;i++) {
            //         if(select_data[i].cTableField_ch == word) {
            //             var re_word = word+'.'+select_data[i].cTableField+'.'+select_data[i].tableClassName;
            //             newStr = newStr.replace(word, re_word);
            //             var re_word_en = select_data[i].cTableField;
            //             newStrEn = newStrEn.replace(word, re_word_en);

                //     }else if(select_data==[]){
                //         alert("不能按确定");
                //     }
                // }
            // }

                });
        console.log(textarea);
        return {
            newStr: newStr,
            newStrEn: newStrEn,
            textarea: textarea,
            select_data: select_data
        };
    }

}
// 取消
function onCancel(){
    console.log(window.parent.KisBpmAssignmentPopupCtrl);
}

function CloseWindow(action) {
    if (window.CloseOwnerWindow)
        return window.CloseOwnerWindow(action);
    else
        window.close();
}
